package com.campus.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.campus.entity.Student;
import com.campus.entity.vo.Student.StudentVO;
import com.campus.mapper.StudentMapper;
import com.campus.service.StudentService;
import com.campus.util.CopyPropertiesUtils;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * @author Alaskaboo
 * @description 针对表【t_student(学生表)】的数据库操作Service实现
 * @createDate 2022-10-24 12:22:31
 */
@Service
public class StudentServiceImpl extends ServiceImpl<StudentMapper, Student> implements StudentService {

    /**
     * 查询所有学生
     *
     * @param student
     * @return
     */
    @Override
    public IPage<StudentVO> selectStudentList(Page<Student> page, Student student) {
        QueryWrapper<Student> queryWrapper = new QueryWrapper<>();
        if (!StringUtils.isEmpty(student.getName())) {
            queryWrapper.like("name", student.getName());
        }
        if (!StringUtils.isEmpty(student.getAccount())) {
            queryWrapper.like("account", student.getAccount());
        }
        if (!StringUtils.isEmpty(student.getIdentity())) {
            queryWrapper.like("identity", student.getIdentity());
        }
        if (!StringUtils.isEmpty(student.getTel())) {
            queryWrapper.like("tel", student.getTel());
        }
        queryWrapper.orderByAsc("id");
        List<Student> studentList = baseMapper.selectList(queryWrapper);
        List<StudentVO> collect = studentList.stream().map(student1 -> CopyPropertiesUtils.copyProperties(student1, StudentVO.class)).collect(Collectors.toList());
        Page<Student> page1 = baseMapper.selectPage(page, queryWrapper);
        Page<StudentVO> resPage = new Page<>();
        BeanUtil.copyProperties(page1,resPage);
        return resPage.setRecords(collect);

    }
}
